<template>
  <a-modal
    title="编辑网关配置模板"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入名称" v-decorator="['configName', {rules: [{required: true, message: '请输入名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入编码" v-decorator="['code', {rules: [{required: true, message: '请输入编码！'}]}]" />
        </a-form-item>

        <a-form-item
          label="值"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入值" v-decorator="['value', {rules: [{required: true, message: '请输入值！'}]}]" />
        </a-form-item>

        <a-form-item
          label="状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group v-decorator="['status',{initialValue: 1 ,rules: [{ required: true, message: '请选择状态！' }]}]" >
            <a-radio :value="1">启用</a-radio>
            <a-radio :value="0">禁用</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入备注" v-decorator="['remark', {rules: [{required: false, message: '请输入备注！'}]}]" />
        </a-form-item>

        <a-form-item
          label="网关配置场景"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select
            style="width: 100%"
            placeholder="请选择网关配置场景"
            v-decorator="['gatewayConfigCode', {rules: [{ required: true, message: '请选择网关配置场景！' }]}]">
            <a-select-option v-for="(item,index) in groupCodeList" :key="index" :value="item.code">{{ item.value }}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/bizgatewayconfigtemplate/bizGatewayConfigTemplateManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        groupCodeList: []
      }
    },
    methods: {
      edit (record) {
        this.visible = true
        this.sysDictTypeDropDown()
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            configName: record.configName,
            code: record.code,
            value: record.value,
            status: record.status,
            remark: record.remark,
            gatewayConfigCode: record.gatewayConfigCode
          })
        }, 100)
        console.log(record)
      },
      sysDictTypeDropDown () {
        sysDictTypeDropDown({ code: 'gateway_config_scene' }).then((res) => {
          if (res.success) {
            this.groupCodeList = res.data
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
